package com.zsinfo.guoss.supplier.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.netcenter.Event.Response;
import com.example.netcenter.RetrofitBuilder;
import com.example.netcenter.Url.ConstantsCode;
import com.example.netcenter.bean.ErrorBean;
import com.example.netcenter.bean.RegisterBean;
import com.example.netcenter.bean.WebSiteBean;
import com.google.gson.Gson;
import com.zsinfo.guoss.supplier.MainActivity;
import com.zsinfo.guoss.supplier.R;
import com.zsinfo.guoss.supplier.base.BaseActivity;
import com.zsinfo.guoss.supplier.customize.WheelView;
import com.zsinfo.guoss.supplier.logichelper.LoginAndRegisterHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lixl on 2018/12/4.
 * <p>
 * desc:注册界面
 */
public class RegisteredActivity extends BaseActivity implements View.OnClickListener {

    private RelativeLayout rl_all; //点击其它地方，隐藏popwindow
    private TextView tv_website;
    private EditText et_username, et_phone, et_address, et_business, et_alipay_account;
    private Button btn_registered;
    private WheelView websiteWheelView;

    private List<String> websites;  //站点列表
    private List<WebSiteBean.DataBean> websiteDatas;
    private int mCurrentSelectedIndex = 0;  //当前选择的站点的下标，对应可以找到站点id,name

    @Override
    protected int getContentLayoutRes() {
        return R.layout.activity_registered;
    }

    @Override
    protected void initView() {
        setTitle("注册");
        setBackAndLeftTitle("返回").setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        rl_all = findViewById(R.id.rl_all);
        tv_website = findViewById(R.id.tv_website);
        et_username = findViewById(R.id.et_username);
        et_phone = findViewById(R.id.et_phone);
        et_address = findViewById(R.id.et_address);
        et_business = findViewById(R.id.et_business);
        et_alipay_account = findViewById(R.id.et_alipay_account);
        btn_registered = findViewById(R.id.btn_registered);
        websiteWheelView = findViewById(R.id.websiteWheelView);

        rl_all.setOnClickListener(this);
        tv_website.setOnClickListener(this);
        btn_registered.setOnClickListener(this);

        et_username.setOnClickListener(this);
        et_phone.setOnClickListener(this);
        et_address.setOnClickListener(this);
        et_business.setOnClickListener(this);
        et_alipay_account.setOnClickListener(this);

        websites = new ArrayList<>();
        websiteDatas = new ArrayList<>();
    }

    @Override
    protected void initNetData() {
        getCityList();
    }

    @Override
    protected void customDestroy() {
        if (websiteWheelView.isShowing()) {
            websiteWheelView.setVisibility(View.INVISIBLE);
            return;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl_all:
            case R.id.et_username:
            case R.id.et_phone:
            case R.id.et_address:
            case R.id.et_business:
            case R.id.et_alipay_account:
                if (websiteWheelView.isShowing()) {
                    websiteWheelView.setVisibility(View.INVISIBLE);
                }
                break;
            case R.id.tv_website:
                websiteWheelView.setVisibility(View.VISIBLE);
                break;
            case R.id.btn_registered:
                //注册
                setRegist();
                break;
        }
    }

    /**
     * 获取城市站点列表
     */
    private void getCityList() {
        GetNetCenter().GetNetCenter(
                RetrofitBuilder.CreateApi().CITYLIST(ConstantsCode.city_list), new Response() {
                    @Override
                    public void OK(Object data) {
                        WebSiteBean webSiteBean = (WebSiteBean) data;
                        Log.e("lixl", data.toString());
                        websiteDatas = webSiteBean.getData();
                        for (int i = 0; i < websiteDatas.size(); i++) {
                            websites.add(websiteDatas.get(i).getWebsiteName());
                        }
                        tv_website.setText(websites.get(0));  //默认显示第一个
                        websiteWheelView.setItems(websites);
                        websiteWheelView.setOffset(1);
                        websiteWheelView.setOnWheelViewListener(new WheelView.OnWheelViewListener() {
                            @Override
                            public void onSelected(int selectedIndex, String item) {
                                tv_website.setText(item);
                                mCurrentSelectedIndex = selectedIndex - 1; //注意，selectedIndex从1开始的

                                Log.e("lixl", "当前站点的下标" + mCurrentSelectedIndex);
                                Log.e("lixl", "站点编码" + websiteDatas.get(mCurrentSelectedIndex).getWebsiteNode());
                                Log.e("lixl", "站点名称" + websiteDatas.get(mCurrentSelectedIndex).getWebsiteName());
                            }
                        });
                    }

                    @Override
                    public void Fail(Throwable e) {
                        Log.e("lixl", e.getMessage());
                    }

                    @Override
                    public void complete() {

                    }
                });
    }

    /**
     * 接口请求，实现注册
     */
    private void setRegist() {
        String website = tv_website.getText().toString().trim();
        String username = et_username.getText().toString().trim();
        String phone = et_phone.getText().toString().trim();
        String address = et_address.getText().toString().trim();
        String business = et_business.getText().toString().trim();
        String alipayAccount = et_alipay_account.getText().toString().trim();
        if (TextUtils.isEmpty(website)) {
            showToast("请选择站点");
            return;
        }
        if (TextUtils.isEmpty(username)) {
            showToast("请输入姓名");
            return;
        }
        if (TextUtils.isEmpty(phone)) {
            showToast("请输入手机号");
            return;
        }
        if (TextUtils.isEmpty(address)) {
            showToast("请输入地址或档口号");
            return;
        }
        if (TextUtils.isEmpty(business)) {
            showToast("请输入主营业务");
            return;
        }
        if (TextUtils.isEmpty(alipayAccount)) {
            showToast("请输入支付宝账号");
            return;
        }
        showToast("注册成功");  //直接跳转到主界面

        Log.e("lixl", "当前站点的下标" + mCurrentSelectedIndex);
        Log.e("lixl", "站点编码" + websiteDatas.get(mCurrentSelectedIndex).getWebsiteNode());
        Log.e("lixl", "站点名称" + websiteDatas.get(mCurrentSelectedIndex).getWebsiteName());
        Log.e("lixl", "姓名" + username);
        Log.e("lixl", "手机号" + phone);
        Log.e("lixl", "地址" + address);
        Log.e("lixl", "主营业务" + business);
        Log.e("lixl", "支付宝账号" + alipayAccount);

        GetNetCenter().GetNetCenter(this,
                RetrofitBuilder.CreateApi().REGIST(
                        ConstantsCode.supplier_regist,
                        websiteDatas.get(mCurrentSelectedIndex).getWebsiteNode(),
                        websiteDatas.get(mCurrentSelectedIndex).getWebsiteName(),
                        username, phone, address, business, alipayAccount), new Response() {
                    @Override
                    public void OK(Object data) {
                        Log.e("lixl", data.toString());
                        showToast("注册成功");
                        RegisterBean registerBean = (RegisterBean) data;
                        LoginAndRegisterHelper loginAndRegisterHelper = new LoginAndRegisterHelper();
                        loginAndRegisterHelper.SaveInfo(registerBean);
                        startActivity(new Intent(RegisteredActivity.this, MainActivity.class));
                        finish();
                    }

                    @Override
                    public void Fail(Throwable e) {
                        Log.e("lixl", e.getMessage());
                        if (e.getMessage().contains("Code")) {
                            ErrorBean errorBean = new Gson().fromJson(e.getMessage(), ErrorBean.class);
                            showToast(errorBean.getMsg());
                        } else {
                            showToast(e.getMessage());
                        }
                    }

                    @Override
                    public void complete() {

                    }
                });
    }
}
